{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bd1647db",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load model directly\n",
    "import pickle as pkl\n",
    "from transformers import AutoTokenizer, AutoModelForCausalLM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "001ab9a5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07eeddcf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "cca16402",
   "metadata": {},
   "source": [
    "# 读取模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f03ce92d",
   "metadata": {},
   "outputs": [],
   "source": [
    "tokenizer = AutoTokenizer.from_pretrained(\"../CodeLlama-7b-hf\")\n",
    "model = AutoModelForCausalLM.from_pretrained(\"../CodeLlama-7b-hf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99764284",
   "metadata": {},
   "outputs": [],
   "source": [
    "generator = pipeline(\"text-generation\", model=model, tokenizer=tokenizer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a361a25",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce07641a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "8a1ec219",
   "metadata": {},
   "source": [
    "# A榜"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ddcbfb78",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('../temp/A_prefixes.pkl', 'rb') as f:\n",
    "    prefixes = pkl.load(f)\n",
    "with open('../temp/A_suffixes.pkl', 'rb') as f:\n",
    "    suffixes = pkl.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6744285c",
   "metadata": {},
   "outputs": [],
   "source": [
    "res = []\n",
    "for p,s in zip(prefixes, suffixes):\n",
    "    output = generator(f\"prefixe is #{}#, prefixe is ${}$, what is middle code ?\", max_length=50, num_return_sequences=1)\n",
    "    res.append(output[0]['generated_text'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ee1d7ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('./res.txt', 'w') as f:\n",
    "    f.write(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24681f70",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "005172d1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "2fd97961",
   "metadata": {},
   "source": [
    "# B榜"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e9845b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('../temp/A_prefixes.pkl', 'rb') as f:\n",
    "    prefixes = pkl.load(f)\n",
    "with open('../temp/A_suffixes.pkl', 'rb') as f:\n",
    "    suffixes = pkl.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5de2214b",
   "metadata": {},
   "outputs": [],
   "source": [
    "res = []\n",
    "for p,s in zip(prefixes, suffixes):\n",
    "    output = generator(f\"prefixe is #{}#, prefixe is ${}$, what is middle code ?\", max_length=50, num_return_sequences=1)\n",
    "    res.append(output[0]['generated_text'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43ab3847",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('./res.txt', 'w') as f:\n",
    "    f.write(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc673818",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0184f3a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7661803",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72f79931",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
